package com.cy.store.controller;


import com.cy.store.service.CartService;
import com.cy.store.util.JsonResult;
import com.cy.store.vo.CartVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("carts")
public class CartController extends BaseController{
    @Autowired
    private CartService cartService;

    @RequestMapping("addToCart")
    public JsonResult<Void> addToCart(Integer pid, Integer amount, HttpSession session){
        cartService.addtoCart(getUidFromSession(session),pid,amount,getUsernameFromSession(session));
//        System.out.println(111111111);
        return new JsonResult<>(OK);
    }
    @RequestMapping({"","/"})
    public JsonResult<List<CartVO>> getVOByUid(HttpSession session){
        List<CartVO> list = cartService.getVOByUid(getUidFromSession(session));
        return new JsonResult<>(OK,list);
    }

    @RequestMapping("{cid}/num/add")
    public JsonResult<Integer> addNum(@PathVariable("cid") Integer cid, HttpSession session){
//        System.out.println(22222222);
        Integer data = cartService.addNum(cid, getUidFromSession(session), getUsernameFromSession(session));
//        System.out.println(33333333);
        return new JsonResult<>(OK,data);
    }

    @RequestMapping("list")
    public JsonResult<List<CartVO>> getVOByCid(HttpSession session,Integer[] cids){
        List<CartVO> list = cartService.getVOByCid(getUidFromSession(session),cids);
        return new JsonResult<>(OK,list);
    }
}
